package LeetCode;

/**
 * @Author: Dijah
 * @Date: 2023/6/30 2:10
 * @Desc: https://leetcode.cn/problems/remove-duplicates-from-sorted-list/submissions/
 *
 * 链表删除重复值
 */
public class code83_deleteDuplicates {

    public static ListNode deleteDuplicates(ListNode head){
        //双指针
        if(head == null) return null;
        ListNode slow = head, fast = head;
        while(fast != null){
            if(fast.val != slow.val){
                //
                slow.next = fast;
                slow = slow.next;
            }
            fast = fast.next;
        }
        //断开与后面的链接
        slow.next = null;
        return head;
    }


    /**
     *
     * 使用c++的方式
     *class Solution {
     * public:
     *     ListNode* deleteDuplicates(ListNode* head) {
     *         if(head == nullptr) return nullptr;
     *         ListNode* slow = head; //当前元素
     *         ListNode* fast = head; // 下一个元素
     *         while(fast != nullptr){
     *             if(fast->val != slow->val){
     *                 slow->next = fast;
     *                 slow = slow->next;
     *             }
     *             fast = fast->next;
     *         }
     *         //断开链接
     *         slow->next = nullptr;
     *         return head;
     *     }
     * };
     *
     *
     */

}


